Repository navigation
Read the API token from CONVERTAPI_TOKEN when none is set in code - #96
Merged
Merged
Conversation
A token set in code (new ConvertApi("...") or ConvertApi.ApiToken) comes
first; otherwise each request reads the CONVERTAPI_TOKEN environment
variable. With neither, uploads, ConvertAsync and GetUserAsync throw a
ConvertApiException with status 401 that says how to set the token, before
anything is sent. Earlier versions uploaded the input files and then got
the API's 401.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The missing-token path throws before StreamContent takes ownership of the supplied FileStream, leaving it open and regressing upload-failure cleanup.
1 open issue
What changed in this PR
Adds environment-based token lookup to the SDK while preserving precedence for tokens set in code and existing constructor validation.
Changes:
- Reads and trims
CONVERTAPI_TOKENwhen no token is set in code. - Rejects authenticated operations locally with a 401
ConvertApiExceptionwhen no token is available. - Adds token-resolution tests and documents the behavior planned for 3.3.0.
| File | Description |
|---|---|
| README.md | Documents automatic token lookup and explicit-constructor validation. |
| ConvertApi/ConvertApiParam.cs | Resolves tokens before stream and remote-file uploads. |
| ConvertApi/ConvertApi.cs | Centralizes token lookup and missing-token errors. |
| ConvertApi.Tests/TokenTests.cs | Tests token precedence, environment fallback, and missing-token behavior. |
| CHANGELOG.md | Records token lookup and validation changes under Unreleased. |
Review effort: Balanced
Resolve the token inside the StreamContent using, so the early missing-token failure still disposes the caller's stream, as a failed upload always did. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Part of 3.3.0. When no token is set in code, the SDK now reads it from the
CONVERTAPI_TOKENenvironment variable, the name the README and the examples already use.What changed
new ConvertApi("...")orConvertApi.ApiToken) always comes first. Otherwise each request readsCONVERTAPI_TOKEN, with surrounding whitespace trimmed.new ConvertApi()now works without a token in the source code.ConvertAsyncandGetUserAsyncthrow aConvertApiExceptionwithStatusCode401. The message says how to set the token and links to the token page, taggedutm_source=dotnet-sdk&utm_content=error-no-token. Before this change, the SDK uploaded the input files first and then got a 401 from the API. The exception type and status code stay the same, so code that handled that 401 keeps working.Responseholds the same text asMessage.new ConvertApi("")andnew ConvertApi(null)still throwArgumentNullException, and the message now namesCONVERTAPI_TOKEN.ConvertApi.TokenEnvironmentVariableis a new public constant holding the variable's name.new ConvertApi()reads the variable by itself. The snippets keepEnvironment.GetEnvironmentVariable("CONVERTAPI_TOKEN"), because they must also work with 3.2.2, which is what nuget.org serves today.[Unreleased]/ Added.How it was tested
dotnet test(9 of 9 pass: the 4 existing tests and 5 new ones inConvertApi.Tests/TokenTests.cs), and pack. The build has the same 3 warnings asmain. NU5048 (PackageIconUrl) was already there and is handled separately in 3.3.0.IConvertApiHttpClientthat records the bearer token of each request. They check that:GetUserAsync;ConvertApiExceptionis thrown and no request is sent;GetUserAsyncwith no token sends nothing;new ConvertApi("")names the variable.The tests restore the static token, the static client and the variable afterwards.
new ConvertApi()with onlyCONVERTAPI_TOKENset convertedExamples/TestFiles/test.docxto a 146,734-byte PDF.GetUserAsyncworked, andDeleteFilesAsyncremoved both server files.ConvertApiExceptionwith the message above, and no upload.🤖 Generated with Claude Code